from django.core.management.base import BaseCommand
from django.db import backend
from django.contrib.comments.models import Comment

class Command(BaseCommand):
    help = "Old comments to new comments migration command"
    
    output_transaction = True
    
    def handle(self, *args, **options):
        migrate_comments()
    
def migrate_comments():
    """
    A helper function that handles the migration of free comments
    and (auth)comments.
    """

    db_wrapper = backend.DatabaseWrapper()
    db_cursor = db_wrapper.cursor()
    db_cursor.execute("SELECT * FROM comments_freecomment;")
    comments = db_cursor.fetchall()

    for c in comments:
        new_comment = Comment(
            content_type_id = c[1],
            object_pk = c[2],
            comment = c[3],
            name = c[4],
            submit_date = c[5],
            is_public = c[6],
            ip_address = c[7],
            site_id = c[9]
        )
        new_comment.save()
    
    db_cursor.execute("SELECT * FROM comments_comment;")
    comments = db_cursor.fetchall()
    
    for c in comments:
        new_comment = Comment(
            content_type = c[2],
            object_pk = c[3],
            comment = c[4],
            user_id = c[1],
            submit_date = c[15],
            is_public = c[16],
            is_removed= c[18],
            ip_address = c[17],
            site_id = c[19],
        )
        new_comment.save()

